<template>
    <div class="login">
        <Particles
            id="tsparticles"
            class="login__particles"
            :options="options"
        />
      <div>
        <div class="container">
          <h1>白夜在线商城</h1>
          <el-form ref="form" :model="admin" :rules="loginRules" label-width="80px">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="admin.username" autocomplete="off" placeholder="请输入用户名"
                        @blur="handleBlur('username')" />
              <template v-slot:error>
                <div v-if="errors.username" class="error">{{ errors.username }}</div>
              </template>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="admin.password" type="password" autocomplete="off" placeholder="请输入密码"
                        @blur="handleBlur('password')" />
              <template v-slot:error>
                <div v-if="errors.password" class="error">{{ errors.password }}</div>
              </template>
            </el-form-item>
            <el-form-item label="验证码" prop="captcha">
              <el-input v-model="admin.captcha" type="text" autocomplete="off" placeholder="请输入验证码"
                        @blur="handleBlur('captcha')" />
              <template v-slot:error>
                <div v-if="errors.captcha" class="error">{{ errors.captcha }}</div>
              </template>
            </el-form-item>
          </el-form>
          <div style="display: flex; justify-content: space-between; width: 300px; margin: 0 auto;">
            <el-image style="width: 120px; height: 38px;" :src="imageBase64Data" @click="getCaptcha"/>
          </div>
          <el-row class="row-bg" justify="center" style="padding-top: 20px;">
            <el-button type="primary" @click="login">登录</el-button>
          </el-row>
        </div>
        <div class="background-container"></div>
      </div>



    </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import router from '@/router';
import { useTokenStore } from '@/stores/token';
import adminApi from "@/api/adminApi.js";
const tokenStore = useTokenStore();
// 登录数据
const admin = ref({
  username: '',
  password: '',
  key: '',
  captcha: ''

});
const imageBase64Data = ref();

//生成验证码
function getCaptcha() {
  adminApi.captcha()
      .then(resp => {
        admin.value.key = resp.data.key;
        imageBase64Data.value = resp.data.imageBase64Data;
      });
}
getCaptcha();

// 表单校验规则
const loginRules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '验证码不能为空', trigger: 'blur' }
  ]
};

function login() {
  adminApi.login(admin.value)
      .then(resp => {
        if (resp.code == 10000) {
          //将JWT保存在tokenStore中
          tokenStore.update(resp.data);
          console.log(resp.data);
          // 跳转
          router.push('/admin/index');
          ElMessage({
            message: '登录成功',
            type: 'success',
            duration: 2000
          });
        } else {
          ElMessage({
            message: resp.msg,
            type: 'error',
            duration: 2000
          });
        }
      });
}

// 错误信息
const errors = ref({
  username: '',
  password: '',
  captcha: ''
});

// 引用 form 实例
const form = ref(null);

const handleBlur = (field) => {
  form.value.validateField(field, (error) => {
    if (error) {
      errors.value[field] = error.message;
    } else {
      errors.value[field] = '';
    }
  });
};





const options = {
    fpsLimit: 60,
    interactivity: {
        detectsOn: 'canvas',
        events: {
            onClick: { // 开启鼠标点击的效果
                enable: true,
                mode: 'push'
            },
            onHover: { // 开启鼠标悬浮的效果(线条跟着鼠标移动)
                enable: true,
                mode: 'grab'
            },
            resize: true
        },
        modes: { // 配置动画效果
            bubble: {
                distance: 400,
                duration: 2,
                opacity: 0.8,
                size: 40
            },
            push: {
                quantity: 4
            },
            grab: {
                distance: 200,
                duration: 0.4
            },
            attract: { // 鼠标悬浮时，集中于一点，鼠标移开时释放产生涟漪效果
                distance: 200,
                duration: 0.4,
                factor: 5
            }
        }
    },
    particles: {
        color: {
            value: 'rgba(244,207,13,0.98)' // 粒子点的颜色
        },
        links: {
            color: '#031051', // 线条颜色
            distance: 150,//线条距离
            enable: true,
            opacity: 0.4, // 不透明度
            width: 1.2 // 线条宽度
        },
        collisions: {
            enable: true
        },
        move: {
            attract: { enable: false, rotateX: 600, rotateY: 1200 },
            bounce: false,
            direction: 'none',
            enable: true,
            out_mode: 'out',
            random: false,
            speed: 0.5, // 移动速度
            straight: false
        },
        number: {
            density: {
                enable: true,
                value_area: 800
            },
            value: 80//粒子数
        },
        opacity: {//粒子透明度
            value: 0.7
        },
        shape: {//粒子样式
            type: 'star'
        },
        size: {//粒子大小
            random: true,
            value: 3
        }
    },
    detectRetina: true
}
</script>

<style scoped>
.login {
    height: 100%;
    width: 100%;
    overflow: hidden;
}
.login__particles {
    height: 100%;
    width: 100%;
    background-size: cover;
    background-repeat: no-repeat;
    background-image: url('@/assets/images/login-bg-3.jpg');
    opacity:0.9;
    position:fixed;
    pointer-events: none;
}
.loginPart{
    position:absolute;
    /*定位方式绝对定位absolute*/
    top:50%;
    left:80%;
    /*顶和高同时设置50%实现的是同时水平垂直居中效果*/
    transform:translate(-50%,-50%);
    /*实现块元素百分比下居中*/
    width:450px;
    padding:50px;
    background: rgba(255,204,255,.3);
    /*背景颜色为黑色，透明度为0.8*/
    box-sizing:border-box;
    /*box-sizing设置盒子模型的解析模式为怪异盒模型，
    将border和padding划归到width范围内*/
    box-shadow: 0px 15px 25px rgba(0,0,0,.5);
    /*边框阴影  水平阴影0 垂直阴影15px 模糊25px 颜色黑色透明度0.5*/
    border-radius:15px;
    /*边框圆角，四个角均为15px*/
}
h2{
    margin:0 0 30px;
    padding:0;
    color: #fff;
    text-align:center;
    /*文字居中*/
}
.btn{
    transform:translate(170px);
    width:80px;
    height:40px;
    font-size:15px;
}
.container {
  position: relative;
  width: 500px;
  border: 2px solid #3498db;
  border-radius: 10px;
  padding: 20px;
  padding-top: 0px;
  margin: auto;
  margin-top: 150px;
  background-color: rgba(240, 248, 255, 0.8);
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
}
</style>